(function (angular) {
    'use strict';

    angular.module('ytong.services').factory('resourceManager', resourceManager);

    function resourceManager(proxyService, $log, growl, Constants){

        var resources = {};
        resources.trucks = [];
        resources.cranes = []
        resources.products = [];
        resources.areas = [];

        function loadResources(url, resourceName){

            resourceName += 's';

            proxyService.get(url)
                .then(function(responseData){
                    $log.info("Resource " + resourceName + " retrieved successfully.");
                    resources[resourceName] = responseData[resourceName];
                }, function(responseData){
                    if(responseData.query_result == Constants.QUERY_RESULTS.NO_RESULTS){
                        resources[resourceName] = [];
                        $log.info("No trucks.");
                    }else {
                        $log.info("Failed retrieving resource " + resourceName + " from server: " + responseData.log_error);
                        growl.error("Failed retrieving resource " + resourceName);
                        resources[resourceName] = [];
                    }
                });
        };

        function getResource(resourceName){
            resourceName += 's';
            return resources[resourceName];
        }

        function addResource(url, resourceData, resourceName) {

            var resource = {};
            resource[resourceName] = resourceData;

            proxyService.addEntity(url,resource)
                .then(function(){
                    growl.success("Resource " + resourceName + " added");
                    loadResources(url,resourceName);
                }, function(){
                    growl.error("Resource " + resourceName + " adding failed");
                });
        };

        function updateResource(url, resourceData, resourceName) {

            var resource = {};
            resource[resourceName] = resourceData;

            proxyService.updateEntity(url,resource)
                .then(function(){
                    growl.success("Resource " + resourceName + " updated");
                    loadResources(url,resourceName);
                }, function() {
                    growl.error("Resource " + resourceName + " updating failed");
                });
        };

        function deleteResource(url, resourceData, resourceName) {

            var resource = {};
            resource.id = resourceData.id;

            proxyService.deleteEntity(url,resource)
                .then(function(){
                    growl.success("Resource " + resourceName + " deleted");
                    loadResources(url,resourceName);
                }, function(){
                    growl.error("Resource " + resourceName + " deleting failed");
                });
        }

        //interface
        return{
            loadResource: loadResources,
            getResource: getResource,
            addResource: addResource,
            updateResource: updateResource,
            deleteResource: deleteResource
        }

    };

}(angular));